package com.coinland.study.view;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;

import com.airbnb.lottie.LottieAnimationView;
import com.airbnb.lottie.LottieComposition;
import com.airbnb.lottie.OnCompositionLoadedListener;
import com.cengalabs.flatui.views.FlatEditText;
import com.coinland.study.R;
import com.coinland.study.base.BaseMvpActivity;
import com.coinland.study.bean.BaseObjectBean;
import com.coinland.study.bean.LoginBean;
import com.coinland.study.contract.LoadLottieContract;
import com.coinland.study.contract.LoginContract;
import com.coinland.study.presenter.LoadLottiePresenter;
import com.coinland.study.presenter.LoginPresenter;
import com.coinland.study.util.Contant;
import com.coinland.study.util.ProgressDialog;
import com.coinland.study.util.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Response;

/**
 * 类描述:
 * 创建人:  shenZ
 * 创建时间: 2019/8/23
 * 联系方式: 252079249@qq.com
 * 修改时间: 2019/8/23
 */
public class LoginActivity extends BaseMvpActivity<LoginPresenter> implements LoginContract.View,LoadLottieContract.View{


    @BindView(R.id.et_username_login)
    FlatEditText etUsernameLogin;
    @BindView(R.id.et_password_login)
    FlatEditText etPasswordLogin;

    @BindView(R.id.lottie_loader)
    LottieAnimationView lottieAnimationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void initView() {
        // lottie动画
        LoadLottiePresenter loadLottiePresenter = new LoadLottiePresenter();
        loadLottiePresenter.attachView(this);
        loadLottiePresenter.loadLottie(Contant.lottieJsonPathI);

        mPresenter = new LoginPresenter();
        mPresenter.attachView(this);
    }


    /**
     * @return 帐号
     */
    private String getUsername() {
        return etUsernameLogin.getText().toString().trim();
    }



    /**
     * @return 密码
     */
    private String getPassword() {
        return etPasswordLogin.getText().toString().trim();
    }





    @Override
    public void showLoading() {
        ProgressDialog.getInstance().show(this);
    }



    @Override
    public void hideLoading() {
        ProgressDialog.getInstance().dismiss();
    }



    @Override
    public void onError(Throwable throwable) {

    }

    @OnClick(R.id.btn_signin_login)
    public void onViewClicked() {
        if (getUsername().isEmpty() || getPassword().isEmpty()) {
            ToastUtil.showShort("帐号密码不能为空",this);
            return;
        }
//        mPresenter.login(getUsername(), getPassword());
        Intent it= new Intent(LoginActivity.this,MainActivity.class);
        startActivity(it);
    }


    @Override
    public void onSuccess(BaseObjectBean<LoginBean> bean) {
        ToastUtil.showShort(bean.getErrorMsg(),this);
    }


    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~ 加载远程JSON动画  ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    @Override
    public void loadError(Throwable throwable) {

    }

    @SuppressLint("RestrictedApi")
    @Override
    public void loadSuccess(Response response) {
        try {
            JSONObject json = new JSONObject(response.body().string());
            LottieComposition.Factory.fromJsonString(json.toString(), new OnCompositionLoadedListener() {
                @Override
                public void onCompositionLoaded(@Nullable LottieComposition composition) {
                    lottieAnimationView.setComposition(composition);
                    lottieAnimationView.playAnimation();
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~ 加载远程JSON动画 结束 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
}
